<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Linked List</title>
    <script>
      
      var LinkedList = function(){
        this.head = null;
        this.size= 0;
      }
      
      var Node = function(poValue,point){
        this.value = null||poValue;
        this.point = null||point;
      }
      
      LinkedList.prototype.searchNode = function(pValue){
        
        var rtnNode = new Node();
        
        if(this.size == 0){
          console.log('This is Empty LinkedList.');
          rtnNode = null;
          return rtnNode;
        }
        
        var head = this.head;
        var temp = this.head;
        
        while(temp.point !== null){
          if(temp.value === pValue){
            rtnNode = temp;
            break;
          }
          temp = temp.point;
        }   
        return rtnNode;
      }
      
      LinkedList.prototype.searchAddNode = function(pInValue, pSeValue){
        var newNode = new Node(pInValue);
        
        var head = this.head;
        var temp = this.head;
        var AddPoint;
        
        // 삽입할 위치가 정해져 있는 경우
        if(!!pSeValue) {
          var voSearch = this.searchNode(pSeValue);
          
          var tempSearch = voSearch.point;
          voSearch.point = newNode;
          newNode.point = tempSearch; 
          
          if(voSearch === null){
            console.log('can not search node!!');
            return;
          }
        // 삽입할 위치가 정해져 있지 않는 경우 (맨마지막에 삽입) 
        } else {
          
          if (head === null){
            this.head = newNode;
          } else {
            while(temp.point !== null) {
              AddPoint = temp.point;
            }
            AddPoint.point = newNode;
          }
          
        } 
        
        this.size++;
        
        return this.size;
        
      }
      
      LinkedList.prototype.searchDelete = function(pDelvalue) {
        
        if(this.size == 0){
          console.log('This is empty LinkedList.');
          return;
        }
        
        
        var head = this.head;
        var temp = this.head;
        var prev = null;
        
        while(temp.point !== null){
          
          if(temp.value === pDelvalue) {
            if(!!prev) {
              prev.Point = temp.point;  
            }
            temp.point = null;
            this.size--;
            break;
          }
          
          prev = temp;
          temp = temp.point;
        }
        
        return this.size;
        
      }
      
      window.onload = function(){
        var vaList = new LinkedList();
        debugger;
        
        console.log('Before Insert data size : ' + vaList.size);
        
        console.log('After Insert data size : ' + vaList.searchAddNode('4'));
        
        console.log('After Insert data size : ' + vaList.searchAddNode('5','4'));
        
        console.log('data : ' + vaList.searchNode('4').value);
        
        console.log('After Delete data size : ' + vaList.searchDelete('5'));
        
        console.log('data : ' + vaList.searchNode('4').value);
        
        console.log('data : ' + vaList.searchNode('5').value);
        
        console.log(vaList);
        
      }
      
    </script>
    
  </head>
  <body>
    <h1>Linked List</h1>
  </body>
</html>
